﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DateConverter.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Defines the DateConverter type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.Silverlight.Converters
{
    using System;
    using System.Windows.Data;

    using Spenta.StreetCare.Silverlight.Resources;

    /// <summary>
    /// Defines the DateConverter type.
    /// </summary>
    public class DateConverter : IValueConverter
    {
        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI.
        /// </summary>
        /// <param name="value">The source data being passed to the target.</param>
        /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>
        /// The value to be passed to the target dependency property.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var date = (DateTime)value;

            if (parameter == null)
            {
                // TimeSpan span = DateTime.Now.ToUniversalTime().Subtract(date);
                // TODO: client - server time difference?.
                TimeSpan span = DateTime.Now.Subtract(date);

                string result;

                if (span.Seconds < 0)
                {
                    result = StreetCare.Now;
                }
                else
                {
                    result = span.Days > 0
                                            ? span.Days == 1 ? StreetCare.Yesterday : string.Format(StreetCare.DaysAgo, span.Days)
                                            : (span.Hours > 0
                                                       ? span.Hours == 1 ? StreetCare.OneHourAgo : string.Format(StreetCare.HoursAgo, span.Hours)
                                                       : (span.Minutes > 0
                                                                  ? span.Minutes == 1 ? StreetCare.OneMinuteAgo : string.Format(StreetCare.MinutesAgo, span.Minutes)
                                                                  : span.Seconds == 1 ? StreetCare.OneSecondAgo : string.Format(StreetCare.SecondsAgo, span.Seconds)));
                }

                return result;
            }

            return date.ToString(parameter.ToString());
        }

        /// <summary>
        /// Modifies the target data before passing it to the source object.  This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
        /// </summary>
        /// <param name="value">The target data being passed to the source.</param>
        /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>
        /// The value to be passed to the source object.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}
